package com.luoke.dao.impl;


import com.luoke.dao.UserPetDao;
import com.luoke.pojo.entity.UserPet;
import com.luoke.util.DatabaseUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserPetDaoImpl implements UserPetDao {

    @Override
    public UserPet getUserPetByPetIdAndUsername(int petId, String userName) {
        UserPet userPet = null;
        String sql = "SELECT up.* FROM user_pet up " +
                "JOIN user u ON up.user_id = u.id " +
                "WHERE up.pet_id = ? AND u.name = ?";

        try (Connection conn = DatabaseUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {

            pstmt.setInt(1, petId);
            pstmt.setString(2, userName);

            try (ResultSet rs = pstmt.executeQuery()) {
                if (rs.next()) {
                    userPet = new UserPet();
                    userPet.setId(rs.getLong("id"));
                    userPet.setUserId(rs.getInt("user_id"));
                    userPet.setPetId(rs.getInt("pet_id"));
                    userPet.setPosition(rs.getInt("position"));
                    userPet.setExp(rs.getInt("exp"));
                    userPet.setLevel(rs.getInt("level"));
                    userPet.setHp(rs.getInt("hp"));
                    userPet.setMp(rs.getInt("mp"));
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
            // 这里可以根据需要进行异常处理，例如记录日志或重新抛出异常
        }

        return userPet;
    }

    @Override
    public void updateLeave(int additionalExp, int newLevel, int petId, String userName) {
        String sql = "UPDATE user_pet up " +
                "JOIN user u ON up.user_id = u.id " +
                "SET up.level = ?, up.exp = ? " +
                "WHERE up.pet_id = ? AND u.name = ?";

        try (Connection conn = DatabaseUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {

            pstmt.setInt(1, newLevel);
            pstmt.setInt(2, additionalExp);
            pstmt.setInt(3, petId);
            pstmt.setString(4, userName);

            int rowsAffected = pstmt.executeUpdate();

            if (rowsAffected > 0) {
                System.out.println("宠物等级和经验值更新成功。");
            } else {
                System.out.println("未找到对应的宠物或用户，更新失败。");
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void updateExp(int experience, int petId, String userName) {
        String sql = "UPDATE user_pet up " +
                "JOIN user u ON up.user_id = u.id " +
                "SET up.exp = ? " +
                "WHERE up.pet_id = ? AND u.name = ?";

        try (Connection conn = DatabaseUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {

            pstmt.setInt(1, experience);
            pstmt.setInt(2, petId);
            pstmt.setString(3, userName);

            int rowsAffected = pstmt.executeUpdate();

            if (rowsAffected > 0) {
                System.out.println("宠物经验值更新成功。");
            } else {
                System.out.println("未找到对应的宠物或用户，更新失败。");
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
